package com.server.model;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/*
 * 端口扫描测试类
 */

public class ScanInterface {
    private static List<String> portList = new ArrayList<>();
    //填充列表网口
    public static void fillList() {
        portList.add("en0");
        portList.add("Intel(R) Wireless-AC 9560 160MHz");
    }

    //扫描并获取列表中可用ip地址
    public static String getIP() {
        for (int i = 0; i < portList.size(); ++i) {
            String curPort = portList.get(i);
            String curIp = getIP(curPort);
            if (!curIp.equals("127.0.0.1"))
                return curIp;
        }
        return  "127.0.0.1";
    }
    //获取指定端口的IP地址
    public static String getIP(String targetInterface) {
        try {
            //得到接口集
            Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface = null;
            //遍历接口集
            while (allNetworkInterfaces.hasMoreElements()) {
                networkInterface = allNetworkInterfaces.nextElement();
                if (networkInterface.getDisplayName().equals(targetInterface)) {
                    Enumeration<InetAddress> addressEnumeration = networkInterface.getInetAddresses();
                    //遍历地址集
                    while(addressEnumeration.hasMoreElements()) {
                        InetAddress inetAddress = addressEnumeration.nextElement();
                        //判断是否为ipv4地址
                        if (inetAddress instanceof Inet4Address)
                            return inetAddress.getHostAddress();
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return  "127.0.0.1";
    }


    public static void scanPort() {
        try {
            //获取所有网络接口
            Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface = null;
            while(allNetworkInterfaces.hasMoreElements()) {
                //获取接口
                networkInterface = allNetworkInterfaces.nextElement();
                System.out.println("NetworkInterface:" + networkInterface.getDisplayName());

                Enumeration<InetAddress> allInetAddresses= networkInterface.getInetAddresses();
                InetAddress inetAddress = null;

                while (allInetAddresses.hasMoreElements()) {
                    inetAddress = allInetAddresses.nextElement();
                    if (inetAddress != null && inetAddress instanceof Inet4Address) {
                        System.out.println("---InetAddress: "+ inetAddress.getHostAddress());
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
